package com.competition.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.competition.dao.AgriculturalTrainingDao;
import com.competition.dao.AgriculturalTrainingDetailDao;
import com.competition.entity.AgriculturalTraining;
import com.competition.entity.AgriculturalTrainingOrder;
import com.competition.entity.AgriculturalTrainingOrderDetail;
import com.competition.mapper.AgriculturalTrainingMapper;
import com.competition.mapper.AgriculturalTrainingOrderDetailMapper;
import com.competition.mapper.AgriculturalTrainingOrderMapper;
import com.competition.resp.ResponseResult;
import com.competition.service.AgriculturalTrainingOrderDetailService;
import com.competition.service.AgriculturalTrainingOrderService;
import com.competition.service.AgriculturalTrainingService;
import com.competition.utils.uuid.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/agriculturalTraining")
public class AgriculturalTrainingController {

    @Autowired
    private AgriculturalTrainingMapper agriculturalTrainingMapper;
    @Autowired
    private AgriculturalTrainingOrderMapper agriculturalTrainingOrderMapper;
    @Autowired
    private AgriculturalTrainingOrderService agriculturalTrainingOrderService;
    @Autowired
    private AgriculturalTrainingOrderDetailMapper agriculturalTrainingOrderDetailMapper;
    @Autowired
    private AgriculturalTrainingOrderDetailService agriculturalTrainingOrderDetailService;
    @Autowired
    private AgriculturalTrainingService agriculturalTrainingService;

    @GetMapping("/getAgriculturalTraining")
    public ResponseResult getAgriculturalTraining(@RequestParam("current") int current) {
        if (current < 0) {
            return new ResponseResult(400, "参数异常", null);
        }
        try {
            List<AgriculturalTrainingDao> allAgriculturalTrainingDao = agriculturalTrainingMapper.getAllAgriculturalTrainingDao(current);
            return new ResponseResult(200, "获取成功", allAgriculturalTrainingDao);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(400, "获取失败", null);
        }
    }

    @GetMapping("/getAgriculturalDetilById")
    public ResponseResult getAgriculturalDetilById(@RequestParam("id") String id) {
        if (id == null) {
            return new ResponseResult(400, "参数异常", null);
        }
        try {
            AgriculturalTrainingDetailDao oneDeatil = agriculturalTrainingMapper.getOneDeatil(id);
            return new ResponseResult(200, "获取成功", oneDeatil);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResponseResult(400, "获取失败", null);
    }

    @GetMapping("/getAgriculturalOrder")
    public ResponseResult getAgriculturalOrder(@RequestParam("openid") String openid, @RequestParam("agriculturalTrainingId") String agriculturalTrainingId) {
        if (openid == null || agriculturalTrainingId == null) {
            return new ResponseResult(400, "参数异常", null);
        }
        try {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("user_id", openid);
            queryWrapper.eq("agricultural_training_id", agriculturalTrainingId);
            AgriculturalTrainingOrder agriculturalTrainingOrder = agriculturalTrainingOrderMapper.selectOne(queryWrapper);
            return new ResponseResult(200, "获取成功", agriculturalTrainingOrder);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(400, "获取失败", null);
        }
    }

    @PostMapping("/addAgriculturalOrder")
    @Transactional(isolation = Isolation.READ_COMMITTED,rollbackFor = Exception.class)
    public ResponseResult addAgriculturalOrder(@RequestBody Map<String, Object> params) {
        if (params.get("openid") == null || params.get("agriculturalTrainingId") == null || params.get("username") == null || params.get("userPhone") == null) {
            return new ResponseResult<Object>(400, "参数错误", null);
        }
        String orderId = UUIDUtil.getUUID();
//        创建订单
        try {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("user_id",(String)params.get("openid"));
            queryWrapper.eq("agricultural_training_id",(String)params.get("agriculturalTrainingId"));
            boolean exists = agriculturalTrainingOrderMapper.exists(queryWrapper);
            if (exists){
                return new ResponseResult<Object>(400, "以注册", null);
            }

            AgriculturalTrainingOrder agriculturalTrainingOrder = new AgriculturalTrainingOrder();
            agriculturalTrainingOrder.setUserId((String) params.get("openid"));
            agriculturalTrainingOrder.setAgriculturalTrainingId((String) params.get("agriculturalTrainingId"));
            agriculturalTrainingOrder.setOrderId(orderId);
            agriculturalTrainingOrderService.save(agriculturalTrainingOrder);

            //获取具体数据
            AgriculturalTrainingDetailDao agriculturalTrainingId = agriculturalTrainingMapper.getOneDeatil((String) params.get("agriculturalTrainingId"));

            //填充订单详情
            AgriculturalTrainingOrderDetail agriculturalTrainingOrderDetail = new AgriculturalTrainingOrderDetail();
            agriculturalTrainingOrderDetail.setAgriculturalTrainingOrderId(orderId);
            agriculturalTrainingOrderDetail.setAgriculturalTrainingAddress(agriculturalTrainingId.getTrainingDetailAddress());
            agriculturalTrainingOrderDetail.setAgriculturalTrainingPrice(agriculturalTrainingId.getAgriculturalTrainingPrice());
            agriculturalTrainingOrderDetail.setAgriculturalTrainingTitle(agriculturalTrainingId.getAgriculturalTrainingTitle());
            agriculturalTrainingOrderDetail.setUserName((String) params.get("username"));
            agriculturalTrainingOrderDetail.setUserPhone((String) params.get("userPhone"));

            boolean save = agriculturalTrainingOrderDetailService.save(agriculturalTrainingOrderDetail);
            if (save) {
                UpdateWrapper updateWrapper = new UpdateWrapper();
                updateWrapper.set("agricultural_training_count",agriculturalTrainingId.getAgriculturalTrainingCount().intValue()+1);
                System.out.println(agriculturalTrainingId.getAgriculturalTrainingCount().intValue()+1);
                updateWrapper.eq("agricultural_training_id",params.get("agriculturalTrainingId"));
                agriculturalTrainingService.update(updateWrapper);
                return new ResponseResult(200, "创建成功", null);
            } else {
                return new ResponseResult(400, "创建失败", null);
            }

        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
//            return new ResponseResult(400, "创建失败", null);
        }
    }

    @GetMapping("/queryAgriculturalTrainingListByCountTop")
    public ResponseResult<Object> queryAgriculturalTrainingListByCountTop(Integer top) throws InterruptedException {
        if (top == null || top <= 0){
            return  new ResponseResult<Object>(400,"参数错误");
        }
        QueryWrapper<AgriculturalTraining> agriculturalTrainingQueryWrapper = new QueryWrapper<>();
        agriculturalTrainingQueryWrapper.orderByDesc("agricultural_training_count");
        agriculturalTrainingQueryWrapper.last("limit " + top);
        List<AgriculturalTraining> list = agriculturalTrainingService.list(agriculturalTrainingQueryWrapper);
        return new ResponseResult<Object>(200,"查询成功",list);
    }
}
